#!/bin/bash
# NDK下交叉编译thrift c++ 库(armeabi-v7a)
# 需要依赖的工具和库
# boost for android
# 如果开启 WITH_OPENSSL,要求　cmake 3.13.5 以上版本,否则要求 3.11.1

sh_folder=$(dirname $(readlink -f $0))

# 检测是否安装NDK,没有安装NDK则报错退出
[ -z "$ANDROID_NDK" ] && echo "ERROR:environment variable ANDROID_NDK not define" && exit 255

# 版本号比较 <
function version_lt() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" != "$1"; }

[ -z "$(which cmake 2>/dev/null)" ] && echo "CMake 3.13.5 or above required" && exit 255
cmake_version=$(cmake --version | grep -E version | awk '{print $3}')

version_lt "$cmake_version" '3.13.5' && echo "CMake 3.13.5 or above required" && exit 255

unameOut="$(uname -s)"
case "${unameOut}" in
    Linux*)     buildPlatform=linux-x86_64;osfamily=Linux;;
    Darwin*)    buildPlatform=darwin-x86;osfamily=Mac;;
    CYGWIN*)    buildPlatform=windows-x86_64;osfamily=Cygwin;;
    MINGW*)     buildPlatform=windows-x86_64;osfamily=MinGw;;
    *)          echo "UNSUPPORTED PLATFORM $osfamily" && exit 255;;
esac
echo buildPlatform=${buildPlatform}

#[[ $osfamily = "Cygwin" || $osfamily = "MinGw" ]] && ANDROID_NDK=$(cygpath -u "$ANDROID_NDK")

ANDROID_TOOLCHAIN_ROOT=$ANDROID_NDK/toolchains/llvm/prebuilt/$buildPlatform

[ ! -d "$ANDROID_TOOLCHAIN_ROOT" ] && echo "NOT EXIST ANDROID_TOOLCHAIN_ROOT=$ANDROID_TOOLCHAIN_ROOT" && exit 255

# 从 source.properties NDK 版本号
NDK_VERSION=$(cat $ANDROID_NDK/source.properties | grep Pkg\.Revision | sed -E 's/\./ /g' | awk '{print $4}')

echo "Android NDK Version:$NDK_VERSION"

# 目标平台
[ -z "$ANDROID_ABI" ] && ANDROID_ABI=armeabi-v7a

# NDK版本要求 19 以上,否则退出
[ "$ANDROID_ABI" != armeabi ] && [ "$NDK_VERSION" -lt 19 ] && echo "Android NKD r19 or above required" && exit 255

# 编译 armeabi 版本 NDK版本要求 16 及以下,否则退出
[ "$ANDROID_ABI" = armeabi ] && [ "$NDK_VERSION" -gt 16 ] && echo "Android NKD r16 or below required" && exit 255

if [ "$ANDROID_ABI" = "armeabi" ]
then
  ARCH=ARMEABI
elif [ "$ANDROID_ABI" = "armeabi-v7a" ]
then
  ARCH=ARMEABI-V7A
elif [ "$ANDROID_ABI" = "arm64-v8a" ]
then
  ARCH=ARM64-V8A
elif [ "$ANDROID_ABI" = "x86" ]
then
  ARCH=X86
elif [ "$ANDROID_ABI" = "x86_64" ]
then
  ARCH=x64
else
  echo "Invalid Android ABI: ${ANDROID_ABI}." 
  exit 255
fi

_boost_root=$sh_folder/dist/boost_1_69_0_android/${ANDROID_ABI}

[ ! -d "$_boost_root" ] && echo "NOT FOUND BOOST dir $_boost_root" && exit 255

echo _boost_root=$_boost_root

additional_opts="-DBoost_NO_SYSTEM_PATHS=ON  \
  -DANDROID_ABI=$ANDROID_ABI \
  -DBoost_COMPILER=-clang \
  -DBoost_ADDITIONAL_VERSIONS=1.69.0;1.69 \
  -DANDROID_STL=c++_static \
  -DCMAKE_CXX_COMPILER_ARCHITECTURE_ID=${ARCH} \
  -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=BOTH \
  -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=BOTH"

THRIFT_ADDITIONAL_OPTS="$additional_opts" \
WITH_BOOST_STATIC=OFF \
WITH_OPENSSL=OFF \
BUILD_COMPILER=OFF \
BOOST_ROOT=$_boost_root \
TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
PREFIX=$sh_folder/dist/thrift_android/$ANDROID_ABI \
PROJECT_FOLDER=$sh_folder/build/thrift_android/$ANDROID_ABI \
BUILD_TYPE=RELEASE \
$sh_folder/build-thrift.sh 
